<?
if (!defined('SMF'))
	die('Hacking attempt...');

global $smcFunc, $txt, $scripturl, $user_info;
global $dbtables, $color_header, $color_line1, $color_line2;
global $ore_price, $ore_delta, $ore_limit;
global $energy_price, $energy_delta, $energy_limit;
global $goods_price, $goods_delta, $goods_limit;
global $organics_price, $organics_delta, $organics_limit;
global $dev_genesis_price, $dev_beacon_price, $dev_emerwarp_price, $dev_warpedit_price;
global $dev_minedeflector_price, $dev_escapepod_price, $dev_fuelscoop_price, $dev_lssd_price;
global $colonist_price, $torpedo_price, $fighter_price, $armor_price;
global $max_emerwarp, $allow_ibank, $inventory_factor, $upgrade_cost;
global $debug;

// Verify user login, if CheckLogin() returns false return to the calling script/function
if (CheckLogin() == false)
	return;

//-------------------------------------------------------------------------------------------------

// Get user info.
$result = $smcFunc['db_query']('', '
	SELECT *
	FROM {raw:db_ships}
	WHERE user_id = {int:user_id}',
	array(
		'db_ships' => $dbtables['ships'],
		'user_id' => $user_info['id'],
	)
);
$playerinfo = $smcFunc['db_fetch_assoc']($result);
$smcFunc['db_free_result']($result);

// fix negative quantities, i guess theres a better way to do but i'm in a hurry
// i dont know how the quantities acutally get negative ...
//
// DOUBLECHECK this
//
if ($playerinfo['ship_ore'] < 0)
{
	$update = $smcFunc['db_query']('', '
		UPDATE {raw:db_ships}
		SET ship_ore = 0
		WHERE user_id = {int:user_id}
		LIMIT 1',
		array(
			'db_ships' => $dbtables['ships'],
			'user_id' => $user_info['id'],
		)
	);
	$playerinfo['ship_ore'] = 0;
}

if ($playerinfo['ship_organics'] < 0)
{
	$update = $smcFunc['db_query']('', '
		UPDATE {raw:db_ships}
		SET ship_organics = 0
		WHERE user_id = {int:user_id}
		LIMIT 1',
		array(
			'db_ships' => $dbtables['ships'],
			'user_id' => $user_info['id'],
		)
	);
	$playerinfo['ship_organics'] = 0;
}

if ($playerinfo['ship_energy'] < 0)
{
	$update = $smcFunc['db_query']('', '
		UPDATE {raw:db_ships}
		SET ship_energy = 0
		WHERE user_id = {int:user_id}
		LIMIT 1',
		array(
			'db_ships' => $dbtables['ships'],
			'user_id' => $user_info['id'],
		)
	);
	$playerinfo['ship_energy'] = 0;
}

if ($playerinfo['ship_goods'] < 0)
{
	$update = $smcFunc['db_query']('', '
		UPDATE {raw:db_ships}
		SET ship_goods = 0
		WHERE user_id = {int:user_id}
		LIMIT 1',
		array(
			'db_ships' => $dbtables['ships'],
			'user_id' => $user_info['id'],
		)
	);
	$playerinfo['ship_goods'] = 0;
}

$result = $smcFunc['db_query']('', '
	SELECT *
	FROM {raw:db_universe}
	WHERE sector_id = {int:sector}',
	array(
		'db_universe' => $dbtables['universe'],
		'sector' => $playerinfo['sector']
	)
);
$sectorinfo = $smcFunc['db_fetch_assoc']($result);
$smcFunc['db_free_result']($result);

if ($sectorinfo['port_ore'] < 0)
{
	$update = $smcFunc['db_query']('', '
		UPDATE {raw:db_universe}
		SET port_ore = 0
		WHERE sector_id = {int:sector}',
		array(
			'db_universe' => $dbtables['universe'],
			'sector' => $playerinfo['sector']
		)
	);
	$sectorinfo['port_ore'] = 0;
}

if ($sectorinfo['port_goods'] < 0)
{
	$update = $smcFunc['db_query']('', '
		UPDATE {raw:db_universe}
		SET port_goods = 0
		WHERE sector_id = {int:sector}',
		array(
			'db_universe' => $dbtables['universe'],
			'sector' => $playerinfo['sector']
		)
	);
	$sectorinfo['port_goods'] = 0;
}

if ($sectorinfo['port_organics'] < 0)
{
	$update = $smcFunc['db_query']('', '
		UPDATE {raw:db_universe}
		SET port_organics = 0
		WHERE sector_id = {int:sector}',
		array(
			'db_universe' => $dbtables['universe'],
			'sector' => $playerinfo['sector']
		)
	);
	$sectorinfo['port_organics'] = 0;
}

if ($sectorinfo['port_energy'] < 0)
{
	$update = $smcFunc['db_query']('', '
		UPDATE {raw:db_universe}
		SET port_energy = 0
		WHERE sector_id = {int:sector}',
		array(
			'db_universe' => $dbtables['universe'],
			'sector' => $playerinfo['sector']
		)
	);
	$sectorinfo['port_energy'] = 0;
}

$result = $smcFunc['db_query']('', '
	SELECT *
	FROM {raw:db_zones}
	WHERE zone_id = {int:zone_id}',
	array(
		'db_zones' => $dbtables['zones'],
		'zone_id' => $sectorinfo['zone_id']
	)
);
$zoneinfo = $smcFunc['db_fetch_assoc']($result);
$smcFunc['db_free_result']($result);

if ($zoneinfo['zone_id'] == 4)
{
	BigTitle($txt['bnt_sector_war']);
	echo $txt['bnt_war_info'], ' <p>';
	return;
}
elseif ($zoneinfo['allow_trade'] == 'N')
{
	BigTitle($txt['bnt_no_trade']);
	echo $txt['bnt_no_trade_info'], '<p>';
	return;
}
elseif ($zoneinfo['allow_trade'] == 'L')
{
	if ($zoneinfo['corp_zone'] == 'N')
	{
		$result = $smcFunc['db_query']('', '
			SELECT team
			FROM {raw:db_ships}
			WHERE ship_id = {int:ship_id}',
			array(
				'db_ships' => $dbtables['ships'],
				'ship_id' => $zoneinfo['owner']
			)
		);
		$ownerinfo = $smcFunc['db_fetch_assoc']($result);
		$smcFunc['db_free_result']($result);
		
		if ($playerinfo['ship_id'] != $zoneinfo['owner'] && $playerinfo['team'] == 0 || $playerinfo['team'] != $ownerinfo['team'])
		{
			BigTitle($txt['bnt_no_trade']);
			echo $txt['bnt_no_trade_out'], '<p>';
			return;
		}
	}
	else
	{
		if ($playerinfo['team'] != $zoneinfo['owner'])
		{
			BigTitle($txt['bnt_no_trade']);
			echo $txt['bnt_no_trade_out'], '<p>';
			return;
		}
	}
}

//-------------------------------------------------------------------------------------------------

if ($sectorinfo['port_type'] != "none" && $sectorinfo['port_type'] != "special")
{
	BigTitle($txt['bnt_title_trade']);

	if ($sectorinfo['port_type'] == "ore")
	{
		$ore_price = $ore_price - $ore_delta * $sectorinfo['port_ore'] / $ore_limit * $inventory_factor;
		$sb_ore = $txt['bnt_selling'];
	}
	else
	{
		$ore_price = $ore_price + $ore_delta * $sectorinfo['port_ore'] / $ore_limit * $inventory_factor;
		$sb_ore = $txt['bnt_buying'];
	}
	
	if ($sectorinfo['port_type'] == "organics")
	{
		$organics_price = $organics_price - $organics_delta * $sectorinfo['port_organics'] / $organics_limit * $inventory_factor;
		$sb_organics = $txt['bnt_selling'];
	}
	else
	{
		$organics_price = $organics_price + $organics_delta * $sectorinfo['port_organics'] / $organics_limit * $inventory_factor;
		$sb_organics = $txt['bnt_buying'];
	}
  
	if ($sectorinfo['port_type'] == "goods")
	{
		$goods_price = $goods_price - $goods_delta * $sectorinfo['port_goods'] / $goods_limit * $inventory_factor;
		$sb_goods = $txt['bnt_selling'];
	}
	else
	{
		$goods_price = $goods_price + $goods_delta * $sectorinfo['port_goods'] / $goods_limit * $inventory_factor;
		$sb_goods = $txt['bnt_buying'];
	}
  
	if ($sectorinfo['port_type'] == "energy")
	{
		$energy_price = $energy_price - $energy_delta * $sectorinfo['port_energy'] / $energy_limit * $inventory_factor;
		$sb_energy = $txt['bnt_selling'];
	}
	else
	{
		$energy_price = $energy_price + $energy_delta * $sectorinfo['port_energy'] / $energy_limit * $inventory_factor;
		$sb_energy = $txt['bnt_buying'];
	}
	
	// Establish default amounts for each commodity
	if ($sb_ore == $txt['bnt_buying'])
		$amount_ore = $playerinfo['ship_ore'];
	else
		$amount_ore = NUM_HOLDS($playerinfo['hull']) - $playerinfo['ship_ore'] - $playerinfo['ship_colonists'];

	if ($sb_organics == $txt['bnt_buying'])
		$amount_organics = $playerinfo['ship_organics'];
	else
		$amount_organics = NUM_HOLDS($playerinfo['hull']) - $playerinfo['ship_organics'] - $playerinfo['ship_colonists'];

	if ($sb_goods == $txt['bnt_buying'])
		$amount_goods = $playerinfo['ship_goods'];
	else
		$amount_goods = NUM_HOLDS($playerinfo['hull']) - $playerinfo['ship_goods'] - $playerinfo['ship_colonists'];

	if ($sb_energy == $txt['bnt_buying'])
		$amount_energy = $playerinfo['ship_energy'];
	else
		$amount_energy = NUM_ENERGY($playerinfo['power']) - $playerinfo['ship_energy'];

	// Limit amounts to port quantities.
	$amount_ore = min($amount_ore, $sectorinfo['port_ore']);
	$amount_organics = min($amount_organics, $sectorinfo['port_organics']);
	$amount_goods = min($amount_goods, $sectorinfo['port_goods']);
	$amount_energy = min($amount_energy, $sectorinfo['port_energy']);

	// Limit amounts to what the player can afford.
	if($sb_ore == $txt['bnt_selling'])
		$amount_ore = min($amount_ore, floor(($playerinfo['credits'] + $amount_organics * $organics_price + $amount_goods * $goods_price + $amount_energy * $energy_price) / $ore_price));

	if($sb_organics == $txt['bnt_selling'])
		$amount_organics = min($amount_organics, floor(($playerinfo['credits'] + $amount_ore * $ore_price + $amount_goods * $goods_price + $amount_energy * $energy_price) / $organics_price));
		
	if($sb_goods == $txt['bnt_selling'])
		$amount_goods = min($amount_goods, floor(($playerinfo['credits'] + $amount_ore * $ore_price + $amount_organics * $organics_price + $amount_energy * $energy_price) / $goods_price));

	if($sb_energy == $txt['bnt_selling'])
		$amount_energy = min($amount_energy, floor(($playerinfo['credits'] + $amount_ore * $ore_price + $amount_organics * $organics_price + $amount_goods * $goods_price) / $energy_price));
	
	echo '
		<script type="text/javascript">
			$(document).ready(function(){
				// Table sorting function
				$(".bnt_table").tablesorter({
					widgets: ["zebra"]
				});
			});
		</script>
		<form action=', $scripturl, '?action=blacknova;sa=port2 method=post>
			<table class="bnt_table" width="100%" cellspacing="1" cellpadding="1">
			<thead>
				<tr>
					<th>', $txt['bnt_commodity'], '</th>
					<th>', $txt['bnt_buying'], '/', $txt['bnt_selling'], '</th>
					<th>', $txt['bnt_amount'], '</th>
					<th>', $txt['bnt_price'], '</th>
					<th>', $txt['bnt_buy'], '/', $txt['bnt_sell'], '</th>
					<th>', $txt['bnt_cargo'], '</th>
				</tr>
			</thead>
			<tbody>
				<tr>
					<td>', $txt['bnt_ore'], '</td>
					<td>', $sb_ore, '</td>
					<td>', NUMBER($sectorinfo['port_ore']), '</td>
					<td>', $ore_price, '</td>
					<td><input type="text" name="trade_ore" size="10" maxlength="20" value="', $amount_ore, '"></td>
					<td>', NUMBER($playerinfo['ship_ore']), '</td>
				</tr>
				<tr>
					<td>', $txt['bnt_organics'], '</td>
					<td>', $sb_organics, '</td>
					<td>', NUMBER($sectorinfo['port_organics']), '</td>
					<td>', $organics_price, '</td>
					<td><input type="text" name="trade_organics" size="10" maxlength="20" value="', $amount_organics, '"></td>
					<td>', NUMBER($playerinfo['ship_organics']), '</td>
				</tr>
				<tr>
					<td>', $txt['bnt_goods'], '</td>
					<td>', $sb_goods, '</td>
					<td>', NUMBER($sectorinfo['port_goods']), '</td>
					<td>', $goods_price, '</td>
					<td><input type="text" name="trade_goods" size="10" maxlength="20" value="', $amount_goods, '"></td>
					<td>', NUMBER($playerinfo['ship_goods']), '</td>
				</tr>
				<tr>
					<td>', $txt['bnt_energy'], '</td>
					<td>', $sb_energy, '</td>
					<td>', NUMBER($sectorinfo['port_energy']), '</td>
					<td>', $energy_price, '</td>
					<td><input type="text" name="trade_energy" size="10" maxlength="20" value="', $amount_energy, '"></td>
					<td>', NUMBER($playerinfo['ship_energy']), '</td>
				</tr>
			</tbody>
		</table><br>
		<input type=submit value=', $txt['bnt_trade'], '>
	</form>';

	$free_holds = NUM_HOLDS($playerinfo['hull']) - $playerinfo['ship_ore'] - $playerinfo['ship_organics'] - $playerinfo['ship_goods'] - $playerinfo['ship_colonists'];
	$free_power = NUM_ENERGY($playerinfo['power']) - $playerinfo['ship_energy'];

	$txt['bnt_trade_st_info'] = str_replace("[free_holds]", NUMBER($free_holds), $txt['bnt_trade_st_info']);
	$txt['bnt_trade_st_info'] = str_replace("[free_power]", NUMBER($free_power), $txt['bnt_trade_st_info']);
	$txt['bnt_trade_st_info'] = str_replace("[credits]", NUMBER($playerinfo['credits']), $txt['bnt_trade_st_info']);
	echo $txt['bnt_trade_st_info'];
}
elseif ($sectorinfo['port_type'] == "special")
{
	BigTitle($txt['bnt_special_port']);
  
	if (isLoanPending($playerinfo['ship_id']))
	{
		echo '<p>', $txt['bnt_port_loannotrade'], '</p>';
		echo '<p><a href="', $scripturl, '?action=blacknova;sa=igb">', $txt['bnt_igb_term'], '</a></p>';
		return;
	}
	
	$result = $smcFunc['db_query']('', '
		SELECT SUM(amount) as total_bounty
		FROM {raw:db_bounty}
		WHERE placed_by = 0
		AND bounty_on = {int:ship_id}',
		array(
			'db_bounty' => $dbtables['bounty'],
			'ship_id' => $playerinfo['ship_id']
		)
	);
	
	if ($smcFunc['db_num_rows']($result) > 0)
	{
		$bty = $smcFunc['db_fetch_assoc']($result);
		
		if ($bty['total_bounty'] > 0)
		{
			if (isset($_GET['pay']))
				$pay = $_GET['pay'];
			else
				$pay = '';
						
			if ($pay <> 1)
			{
				echo '<p>', $txt['bnt_port_bounty'], '</p>';
				$txt['bnt_port_bounty2'] = str_replace("[amount]", NUMBER($bty['total_bounty']), $txt['bnt_port_bounty2']);
				echo '<p>', $txt['bnt_port_bounty2'], '</p>';
				echo '<p><a href="', $scripturl, '?action=blacknova;sa=bounty">', $txt['bnt_by_placebounty'], '</a></p>';
				return;
			}
	        else
	        {
				if ($playerinfo['credits'] < $bty['total_bounty'])
				{
					$txt['bnt_port_btynotenough'] = str_replace("[amount]", NUMBER($bty['total_bounty']), $txt['bnt_port_btynotenough']);
					echo '<p>', $txt['bnt_port_btynotenough'], '</p>';
					return;
				}
				else
				{
					echo '<p>', $txt['bnt_total_bounty'], ': ', $bty['total_bounty'], '</p>';
					
					$update = $smcFunc['db_query']('', '
						UPDATE {raw:db_ships}
						SET credits = credits - {int:total_bounty}
						WHERE ship_id = {int:ship_id}',
						array(
							'db_ships' => $dbtables['ships'],
							'total_bounty' => $bty['total_bounty'],
							'ship_id' => $playerinfo['ship_id']
						)
					);
					
					$delete = $smcFunc['db_query']('', '
						DELETE FROM {raw:db_bounty}
						WHERE bounty_on = {int:ship_id}
						AND placed_by = 0',
						array(
							'db_bounty' => $dbtables['bounty'],
							'ship_id' => $playerinfo['ship_id']
						)
					);
					
					echo '<p>', $txt['bnt_port_bountypaid'], '</p>';
					return;
				}
			}
		}	
	}
	$smcFunc['db_free_result']($result);
		
	$emerwarp_free = $max_emerwarp - $playerinfo['dev_emerwarp'];
	$fighter_max = NUM_FIGHTERS($playerinfo['computer']);
	$fighter_free = $fighter_max - $playerinfo['ship_fighters'];
	$torpedo_max = NUM_TORPEDOES($playerinfo['torp_launchers']);
	$torpedo_free = $torpedo_max - $playerinfo['torps'];
	$armor_max = NUM_ARMOUR($playerinfo['armor']);
	$armor_free = $armor_max - $playerinfo['armor_pts'];
	$colonist_max = NUM_HOLDS($playerinfo['hull']) - $playerinfo['ship_ore'] - $playerinfo['ship_organics'] - $playerinfo['ship_goods'];
	$colonist_free = $colonist_max - $playerinfo['ship_colonists'];

	TEXT_JAVASCRIPT_BEGIN();

	echo '
		function countTotal()
		{';
	
	// Here we cycle through all form values (other than buy, or full), and regexp out all non-numerics. (1,000 = 1000)
	// Then, if its become a null value (type in just a, it would be a blank value. blank is bad.) we set it to zero.
	echo '
			var form = document.forms[1];
			var i = form.elements.length;
			while (i > 0)
			{
				if ((form.elements[i-1].value != \'Buy\') && (form.elements[i-1].value != \'Full\'))
				{
					var tmpval = form.elements[i-1].value.replace(/\D+/g, "");
					if (tmpval != form.elements[i-1].value)
					{
						form.elements[i-1].value = form.elements[i-1].value.replace(/\D+/g, "");
					}
				}			
				if (form.elements[i-1].value == \'\')
				{
					form.elements[i-1].value =\'0\';
				}
				i--;
			}';
			
	// Here we set all 'Max' items to 0 if they are over max - player amt.
	echo '
			if ((', $emerwarp_free, ' < form.dev_emerwarp_number.value) && (form.dev_emerwarp_number.value != \'Full\'))
			{
				form.dev_emerwarp_number.value = 0
			}
			if ((', $fighter_free, ' < form.fighter_number.value) && (form.fighter_number.value != \'Full\'))
			{
				form.fighter_number.value = 0
			}
			if ((', $torpedo_free, ' < form.torpedo_number.value) && (form.torpedo_number.value != \'Full\'))
			{
				form.torpedo_number.value = 0
			}
			if ((', $armor_free, ' < form.armor_number.value) && (form.armor_number.value != \'Full\'))
			{
				form.armor_number.value = 0
			}
			if ((', $colonist_free, ' < form.colonist_number.value) && (form.colonist_number.value != \'Full\' ))
			{
				form.colonist_number.value = 0
			}';

	// Done with the bounds checking
	// Pluses must be first, or if empty will produce a javascript error
	echo '
			form.total_cost.value = form.dev_genesis_number.value * ', $dev_genesis_price, ' 
				+ form.dev_beacon_number.value * ', $dev_beacon_price;

	if ($emerwarp_free > 0)
		echo '	+ form.dev_emerwarp_number.value * ', $dev_emerwarp_price;

	echo '
				+ form.dev_warpedit_number.value * ', $dev_warpedit_price, '
				+ form.elements[\'dev_minedeflector_number\'].value * ', $dev_minedeflector_price;

	if ($playerinfo['dev_escapepod'] == 'N')
		echo '	+ (form.escapepod_purchase.checked ?  ', $dev_escapepod_price , ': 0)';

	if ($playerinfo['dev_fuelscoop'] == 'N')
		echo '	+ (form.fuelscoop_purchase.checked ?  ', $dev_fuelscoop_price, ' : 0)';

	if ($playerinfo['dev_lssd'] == 'N')
		echo '	+ (form.lssd_purchase.checked ?  ', $dev_lssd_price, ' : 0)';
	
	echo '
				+ changeDelta(form.hull_upgrade.value, ', $playerinfo['hull'], ', ', $upgrade_cost,')
				+ changeDelta(form.engine_upgrade.value, ', $playerinfo['engines'], ', ', $upgrade_cost,')
				+ changeDelta(form.power_upgrade.value, ', $playerinfo['power'], ', ', $upgrade_cost,')
				+ changeDelta(form.computer_upgrade.value, ', $playerinfo['computer'], ', ', $upgrade_cost,')
				+ changeDelta(form.sensors_upgrade.value, ', $playerinfo['sensors'], ', ', $upgrade_cost,')
				+ changeDelta(form.beams_upgrade.value, ', $playerinfo['beams'], ', ', $upgrade_cost,')
				+ changeDelta(form.armor_upgrade.value, ', $playerinfo['armor'], ', ', $upgrade_cost,')
				+ changeDelta(form.cloak_upgrade.value, ', $playerinfo['cloak'], ', ', $upgrade_cost,')
				+ changeDelta(form.torp_launchers_upgrade.value, ', $playerinfo['torp_launchers'], ', ', $upgrade_cost,')
				+ changeDelta(form.shields_upgrade.value, ', $playerinfo['shields'], ', ', $upgrade_cost,')';

	if ($playerinfo['ship_fighters'] != $fighter_max)
		echo '	+ form.fighter_number.value * ', $fighter_price;

	if ($playerinfo['torps'] != $torpedo_max)
	    echo '	+ form.torpedo_number.value *', $torpedo_price;

	if ($playerinfo['armor_pts'] != $armor_max)
	    echo '	+ form.armor_number.value *', $armor_price;

	if ($playerinfo['ship_colonists'] != $colonist_max)
	    echo '	+ form.colonist_number.value *', $colonist_price;
	
	echo '
			;
			if (form.total_cost.value > ', $playerinfo['credits'], ')
			{
				form.total_cost.value = \'', $txt['bnt_no_credits'], '\'';
				
	// form.total_cost.value = 'You are short '+(form.total_cost.value - $playerinfo[credits]) +' credits';
	
	echo '
			}
		
			form.total_cost.length = form.total_cost.value.length;
			form.engine_costper.value = changeDelta(form.engine_upgrade.value,', $playerinfo['engines'], ', ', $upgrade_cost,')
			form.power_costper.value = changeDelta(form.power_upgrade.value,', $playerinfo['power'], ', ', $upgrade_cost,')
			form.computer_costper.value = changeDelta(form.computer_upgrade.value,', $playerinfo['computer'], ', ', $upgrade_cost,')
			form.sensors_costper.value = changeDelta(form.sensors_upgrade.value,', $playerinfo['sensors'], ', ', $upgrade_cost,')
			form.beams_costper.value = changeDelta(form.beams_upgrade.value,', $playerinfo['beams'], ', ', $upgrade_cost,')
			form.armor_costper.value = changeDelta(form.armor_upgrade.value,', $playerinfo['armor'], ', ', $upgrade_cost,')
			form.cloak_costper.value = changeDelta(form.cloak_upgrade.value,', $playerinfo['cloak'], ', ', $upgrade_cost,')
			form.torp_launchers_costper.value = changeDelta(form.torp_launchers_upgrade.value,', $playerinfo['torp_launchers'], ', ', $upgrade_cost,')
			form.hull_costper.value = changeDelta(form.hull_upgrade.value,', $playerinfo['hull'], ', ', $upgrade_cost,')
			form.shields_costper.value = changeDelta(form.shields_upgrade.value,', $playerinfo['shields'], ', ', $upgrade_cost,')
		}';

	TEXT_JAVASCRIPT_END();

	$onblur = 'onblur="countTotal();"';
	$onfocus = 'onfocus="countTotal();"';
	$onchange = 'onchange="countTotal();"';
	$onclick = 'onclick="countTotal();"';

	// Create dropdowns when called
	function dropdown($element_name, $current_value)
	{
		$onchange = 'onchange="countTotal();"';
		
		$i = $current_value;
		$dropdownvar = '<select size="1" name="' . $element_name . '" ' . $onchange . '>';

		while ($i < 60)
		{
			if ($current_value == $i)
				$dropdownvar = $dropdownvar . '<option value="' . $i . '" selected>' . $i . '</option>';
			else
				$dropdownvar = $dropdownvar . '<option value="' . $i . '">' . $i . '</option>';
			$i++;
		}

		$dropdownvar = $dropdownvar . '</select>';
		return $dropdownvar;
	}

	$txt['bnt_creds_to_spend'] = str_replace("[credits]", NUMBER($playerinfo['credits']), $txt['bnt_creds_to_spend']);
	echo '
		<p>', $txt['bnt_creds_to_spend'], '</p>';
	  
	if ($allow_ibank)
	{
		$igblink = '<a href="' . $scripturl . '?action=blacknova;sa=igb">' . $txt['bnt_igb_term'] . '</a>';
		$txt['bnt_ifyouneedmore'] = str_replace("[igb]", $igblink, $txt['bnt_ifyouneedmore']);

		echo '
		<p>', $txt['bnt_ifyouneedmore'], '</p>';
	}

	echo '
		<script type="text/javascript">
			$(document).ready(function(){
				// Table sorting function
				$(".bnt_table").tablesorter({
					widgets: ["zebra"]
				});
			});
		</script>
		<p><a href="', $scripturl, '?action=blacknova;sa=bounty">', $txt['bnt_by_placebounty'], '</a></p>
		<form action="', $scripturl, '?action=blacknova;sa=port2" method="post">
		<table class="bnt_table">
			<thead>
				<tr>
					<th>', $txt['bnt_device'], '</th>
					<th>', $txt['bnt_cost'], '</th>
					<th>', $txt['bnt_current'], '</th>
					<th>', $txt['bnt_max'], '</th>
					<th>', $txt['bnt_qty'], '</th>
				</tr>
			</thead>
			<tbody>
				<tr>
					<td>', $txt['bnt_genesis'], '</td>
					<td>', NUMBER($dev_genesis_price), '</td>
					<td>', NUMBER($playerinfo['dev_genesis']), '</td>
					<td>', $txt['bnt_unlimited'], '</td>
					<td><input type="text" name="dev_genesis_number" size="4" maxlength="4" value="0" ', $onblur, '></td>
				</tr>
				<tr>
					<td>', $txt['bnt_beacons'], '</td>
					<td>', number($dev_beacon_price), '</td>
					<td>', number($playerinfo['dev_beacon']), '</td>
					<td>', $txt['bnt_unlimited'], '</td>
					<td><input type="text" name="dev_beacon_number" size="4" maxlength="4" value="0" ', $onblur, '></td>
				</tr>
				<tr>
					<td>', $txt['bnt_ewd'], '</td>
					<td>', number($dev_emerwarp_price), '</td>
					<td>', number($playerinfo['dev_emerwarp']), '</td>
					<td>';
	  
	if ($playerinfo['dev_emerwarp'] != $max_emerwarp)
	{
		echo '
					<span onclick="makemax(\'dev_emerwarp_number\', ', $emerwarp_free, '); countTotal(); return false;">
						', number($emerwarp_free), '</span></td>
					<td class="buy_max"><input type="text" id="dev_emerwarp_number" name="dev_emerwarp_number" size="4" maxlength="4" value="0" ', $onblur, '>';
	}
	else
	{
		echo '0</td>
					<td><input type="text" readonly class="portcost" id="dev_emerwarp_number" name="dev_emerwarp_number" size="5" value="', $txt['bnt_full'], '" ', $onblur, ' tabindex="-1">';
	}

	echo '
					</td>
				</tr>
				<tr >
					<td>', $txt['bnt_warpedit'], '</td>
					<td>', number($dev_warpedit_price), '</td>
					<td>', number($playerinfo['dev_warpedit']), '</td><td>', $txt['bnt_unlimited'], '</td>
					<td><input type="text" name="dev_warpedit_number" size="4" maxlength="4" value="0" ', $onblur, '></td>
				</tr>
				<tr>
					<td>', $txt['bnt_deflect'], '</td>
					<td>', number($dev_minedeflector_price), '</td>
					<td>', number($playerinfo['dev_minedeflector']), '</td>
					<td>', $txt['bnt_unlimited'], '</td>
					<td><input type="text" name="dev_minedeflector_number" size="4" maxlength="10" value="0" ', $onblur, '></td>
				</tr>
				<tr>
					<td>', $txt['bnt_escape_pod'], '</td>
					<td>', number($dev_escapepod_price), '</td>';
	  
	if ($playerinfo['dev_escapepod'] == "N")
		echo '		
					<td>', $txt['bnt_none'], '</td>
					<td>&nbsp;</td>
					<td><input style="check" type="checkbox" name="escapepod_purchase" value="1" ', $onclick, '></td>';
	else
		echo '		
					<td>', $txt['bnt_equipped'], '</td>
					<td></td>
					<td></td>';
	
	echo '
				</tr>
				<tr>
					<td>', $txt['bnt_fuel_scoop'], '</td>
					<td>', number($dev_fuelscoop_price), '</td>';

	if ($playerinfo['dev_fuelscoop'] == "N")
	{
		echo '		
					<td>', $txt['bnt_none'], '</td>
					<td>&nbsp;</td>
					<td><input style="check" type="checkbox" name="fuelscoop_purchase" value="1" ', $onclick, '></td>';
	}
	else
	{
		echo '		
					<td>', $txt['bnt_equipped'], '</td>
					<td></td>
					<td></td>';
	}
	  
	echo '			
				</tr>
				<tr>
					<td>', $txt['bnt_lssd'], '</td>
					<td>', number($dev_lssd_price), '</td>';

	if ($playerinfo['dev_lssd'] == "N")
	{
		echo '		
					<td>', $txt['bnt_none'], '</td>
					<td>&nbsp;</td>
					<td><input style="check" type="checkbox" name="lssd_purchase" value="1" ', $onclick, '></td>';
	}
	else
	{
		echo '		
					<td>', $txt['bnt_equipped'], '</td>
					<td></td>
					<td></td>';
	}

	echo '			
				</tr>
			</tbody>
		</table>
		<table class="bnt_table">
			<thead>
				<tr>
					<th>', $txt['bnt_ship_levels'], '</th>
					<th>', $txt['bnt_cost'], '</th>
					<th>', $txt['bnt_current'], '</th>
					<th>', $txt['bnt_upgrade'], '</th>
				</tr>
			</thead>
			<tbody>
				<tr>
					<td>', $txt['bnt_hull'], '</td>
					<td><input type="text" readonly class="portcost" name="hull_costper" value="0" tabindex="-1" ', $onblur, '></td>
					<td>', NUMBER($playerinfo['hull']), '</td>
					<td>', dropdown("hull_upgrade",$playerinfo['hull']), '</td>
				</tr>
				<tr>
					<td>', $txt['bnt_engines'], '</td>
					<td><input type="text" readonly class="portcost" name="engine_costper" value="0" tabindex="-1" ', $onblur, '></td>
					<td>', number($playerinfo['engines']), '</td>
					<td>', dropdown("engine_upgrade",$playerinfo['engines']), '</td>
				</tr>
				<tr>
					<td>', $txt['bnt_power'], '</td>
					<td><input type="text" readonly class="portcost" name="power_costper" value="0" tabindex="-1" ', $onblur, '></td>
					<td>', number($playerinfo['power']), '</td>
					<td>', dropdown("power_upgrade",$playerinfo['power']), '</td>
				</tr>
				<tr>
					<td>', $txt['bnt_computer'], '</td>
					<td><input type="text" readonly class="portcost" name="computer_costper" value="0" tabindex="-1" ', $onblur, '></td>
					<td>', number($playerinfo['computer']), '</td>
					<td>', dropdown("computer_upgrade", $playerinfo['computer']), '</td>
				</tr>
				<tr>
					<td>', $txt['bnt_sensors'], '</td>
					<td><input type="text" readonly class="portcost" name="sensors_costper" value="0" tabindex="-1" ', $onblur, '></td>
					<td>', number($playerinfo['sensors']), '</td>
					<td>', dropdown("sensors_upgrade", $playerinfo['sensors']), '</td>
				</tr>
				<tr>
					<td>', $txt['bnt_beams'], '</td>
					<td><input type="text" readonly class="portcost" name="beams_costper" value="0" tabindex="-1" ', $onblur, '></td>
					<td>', number($playerinfo['beams']), '</td>
					<td>', dropdown("beams_upgrade",$playerinfo['beams']), '</td>
				</tr>
				<tr>
					<td>', $txt['bnt_armor'], '</td>
					<td><input type="text" readonly class="portcost" name="armor_costper" value="0" tabindex="-1" ', $onblur, '></td>
					<td>', number($playerinfo['armor']), '</td>
					<td>', dropdown("armor_upgrade",$playerinfo['armor']), '</td>
				</tr>
				<tr>
					<td>', $txt['bnt_cloak'], '</td>
					<td><input type="text" readonly class="portcost" name="cloak_costper" value="0" tabindex="-1" ', $onblur, ' ' , $onfocus, '></td>
					<td>', number($playerinfo['cloak']), '</td>
					<td>', dropdown("cloak_upgrade",$playerinfo['cloak']), '</td>
				</tr>
				<tr>
					<td>', $txt['bnt_torp_launch'], '</td>
					<td><input type="text" readonly class="portcost" name="torp_launchers_costper" value="0" tabindex="-1" ', $onblur, '></td>
					<td>', number($playerinfo['torp_launchers']), '</td>
					<td>', dropdown("torp_launchers_upgrade", $playerinfo["torp_launchers"]), '</td>
				</tr>
				<tr>
					<td>', $txt['bnt_shields'], '</td>
					<td><input type="text" readonly class="portcost" name="shields_costper" value="0" tabindex="-1" ', $onblur, '></td>
					<td>', number($playerinfo['shields']), '</td>
					<td>', dropdown("shields_upgrade",$playerinfo['shields']), '</td>
				</tr>
			</tbody>
		</table>
		<table class="bnt_table">
			<thead>
				<tr>
					<th>', $txt['bnt_item'], '</th>
					<th>', $txt['bnt_cost'], '</th>
					<th>', $txt['bnt_current'], '</th>
					<th>', $txt['bnt_max'], '</th>
					<th>', $txt['bnt_qty'], '</th>
				</tr>
			</thead>
			<tbody>
				<tr>
					<td>', $txt['bnt_fighters'], '</td>
					<td>', number($fighter_price), '</td>
					<td>', number($playerinfo['ship_fighters']), ' / ', number($fighter_max), '</td>
					<td class="buy_max">';

	if ($playerinfo['ship_fighters'] != $fighter_max)
	{
		echo '			<span onclick="makemax(\'fighter_number\', ', $fighter_free, '); countTotal(); return false;"; ', $onblur, '>', number($fighter_free), '</span></td>
					<td><input type="text" id="fighter_number" name="fighter_number" size="6" maxlength="10" value="0" ', $onblur, '>';
	}
	else
		echo '		0<td><input type="text" readonly class="portcost" name="fighter_number" maxlength="10" value="', $txt['bnt_full'], '" ', $onblur, ' tabindex="-1">';
	  
	echo '			</td>
				</tr>
				<tr>
					<td>', $txt['bnt_torps'], '</td>
					<td>', number($torpedo_price), '</td>
					<td>', number($playerinfo['torps']), ' / ', number($torpedo_max), '</td>
					<td class="buy_max">';
	  
	if ($playerinfo['torps'] != $torpedo_max)
	{
		echo '			<span onclick="makemax(\'torpedo_number\', ', $torpedo_free, '); countTotal(); return false;"; ', $onblur,'>', number($torpedo_free), '</span></td>
					<td><input type="text" id="torpedo_number" name="torpedo_number" size="6" maxlength="10" value="0" ', $onblur, '>';
	}
	else
		echo '		0<td><input type="text" readonly class="portcost" name="torpedo_number" maxlength="10" value="', $txt['bnt_full'] , '" ' , $onblur, ' tabindex="-1">';
	  
	echo '			</td>
				</tr>
				<tr>
					<td>', $txt['bnt_armorpts'], '</td>
					<td>', number($armor_price), '</td>
					<td>', number($playerinfo['armor_pts']), ' / ',  number($armor_max), '</td>
					<td>';

	if ($playerinfo['armor_pts'] != $armor_max)
	{
		echo '			<span onclick="makemax(\'armor_number\', ', $armor_free, '); countTotal(); return false;"; ', $onblur, '>', number($armor_free), '</span></td>
					<td class="buy_max"><input type="text" id="armor_number" name=armor_number size=6 maxlength=10 value=0 ', $onblur, '>';
	}
	else
		echo '		0<td><input type=text readonly class="portcost" name=armor_number maxlength=10 value=', $txt['bnt_full'], ' tabindex="-1" ', $onblur, '>';
	  
	echo '			</td>
				</tr>
				<tr>
					<td>', $txt['bnt_colonists'], '</td>
					<td>', number($colonist_price), '</td>
					<td>', number($playerinfo['ship_colonists']), ' / ', number($colonist_max), '</td>
					<td class="buy_max">';
	  
	if($playerinfo['ship_colonists'] != $colonist_max)
	{
		echo '			<span onclick="makemax(\'colonist_number\', ', $colonist_free, '); countTotal(); return false;"; $onblur>', number($colonist_free), '</span></td>
					<td><input type=text id="colonist_number" name=colonist_number size=6 maxlength=10 value=0 ', $onblur, '>';
	}
	else
		echo '		0<td><input type=text readonly class="portcost" name=colonist_number maxlength=10 value=', $txt['bnt_full'], ' tabindex="-1" ', $onblur, '>';
	  
	echo '			</td>
				</tr>
			</tbody>
		</table><br>
		<table width="100%" border=0 cellspacing=0 cellpadding=0>
			<tr>
				<td><input type=submit value=', $txt['bnt_buy'], ' ', $onclick, '></td>
				<td align=right>', $txt['bnt_totalcost'], ': <input type=text style="text-align:right" name=total_cost size=22 value=0 ', $onfocus, ' ', $onblur, ' ', $onchange, ' ', $onclick, '></td>
			</tr>
		</table>
		</form>
			', $txt['bnt_would_dump'], '<a href="', $scripturl, '?action=blacknova;sa=dump">', $txt['bnt_here'], '</a>.';
}
else
	BigTitle($txt['bnt_noport']);
?>